﻿using System;
using System.Collections.Generic;
using FireFly;
using System.Reflection;
using System.Collections;
using System.Windows;
using System.ComponentModel;
using System.Windows.Documents;

namespace XAMLStylePad.ViewModels
{
    public abstract class VariableViewModel : IViewModel
    {
        object _value;
        TypeViewModel _TypeViewModel = null;
        string _ToggleObjectButtonCaption = "new...";
        bool _IsDefined = false;

        public abstract Type Type
        {
            get;
        }

        public bool IsDefined
        {
            get
            {
                return _IsDefined;
            }
            set
            {
                _IsDefined = value;
                OnPropertyChanged("IsDefined");
            }
        }

        public TypeViewModel TypeViewModel
        {
            get
            {
                if(_TypeViewModel == null && (Type.IsClass || IsStruct(Type)) &&
                    Type != typeof(string) && Type != typeof(DateTime))
                {
                    _TypeViewModel = new TypeViewModel(Type);
                }
                return _TypeViewModel;
            }
        }

        public static bool IsStruct(Type type)
        {
            return !type.IsPrimitive && type.IsValueType && !type.IsEnum && type != typeof(decimal);
        }

        public ICollection PossibleValues
        {
            get
            {
                if(Type.IsEnum)
                {
                    return Enum.GetValues(Type);
                }
                return null;
            }
        }

        public string ToolTip
        {
            get
            {
                return Type.FullName + " " + Name;
            }
        }

        public abstract string Name
        {
            get;
        }

        public object Value
        {
            get
            {
                if(_value == null && IsDefined && TypeViewModel != null)
                {
                    _value = TypeViewModel.CreateInstance();
                }
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        #region ToggleObjectButtonCaption
        public string ToggleObjectButtonCaption 
        { 
            get
            {
                return _ToggleObjectButtonCaption;
            } 
            set
            {
                _ToggleObjectButtonCaption = value;OnPropertyChanged("ToggleObjectButtonCaption");
            }
        }
        #endregion


        #region ToggleObjectCommand
        public IRelayCommand ToggleObjectCommand { get { return _ToggleObjectCommand; } }
        static RelayCommand<VariableViewModel> _ToggleObjectCommand = new RelayCommand<VariableViewModel>(context =>
        {
            var defined = !context.ViewModel.IsDefined;
            context.ViewModel.IsDefined = defined;

            if(defined)
                context.ViewModel.ToggleObjectButtonCaption = "set to null";
            else
                context.ViewModel.ToggleObjectButtonCaption = "new...";

            return;

        }, "VariableViewModel.ToggleObjectCommand");
        #endregion



        #region IViewModel Members
        public IEnumerable<IRelayCommand> Commands
        {
            get { throw new NotImplementedException(); }
        }

        public string DisplayName
        {
            get
            {
                return Name;
            }
        }

        public abstract bool IsReadOnly
        {
            get;
        }

        public IRelayCommand SelectCommand
        {
            get { throw new NotImplementedException(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string prop)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        #endregion
    }
}